//
//  HJReportVC.m
//  project
//
//  Created by jc on 2019/5/13.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJReportVC.h"
#import "HJTeasingVC.h"

@interface HJReportVC ()
@property (nonatomic,strong) NSArray<HJTeasingModel *>*titleArray;
@property (nonatomic,strong) NSIndexPath * cellRow;
@end
static NSString * const identifier = @"ReportCell";
@implementation HJReportVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"匿名举报";
    self.titleArray = [NSArray array];
    [self loadNetworkData];
}

-(void)configTableView {
    self.needHeaderRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.tableHeaderView = [self tableViewHeaderVew];
    self.tableView.tableHeaderView.height = 46;
    self.tableView.rowHeight = 48;
    
    self.cellRow = [NSIndexPath indexPathForRow:0 inSection:0];
    
    [self setUpBottomView];
}

-(UIView *)tableViewHeaderVew {
    UILabel * title =[UILabel new];
    title.text= @"告诉我们举报原因，让乐见世界更美好~";
    title.textAlignment = NSTextAlignmentCenter;
    title.backgroundColor = [UIColor clearColor];
    title.textColor = [UIColor colorWithHex:0x999999];
    title.font = [UIFont systemFontOFSize13];
    title.frame = CGRectMake(0, 0, SCREEN_WIDTH, 46);
    
    return title;
}

-(void)loadNetworkData {
    NSString * url = [Api stringByAppendingString:reportListApi];
    [HJHttpTool get:url params:nil success:^(HJResponeds *result) {
        self.titleArray = [NSArray yy_modelArrayWithClass:[HJTeasingModel class] json:result.data];
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(id result) {
        [self showErrorHUDWithError:result];
        [self.tableView endRefresh];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJReportCell * reportCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!reportCell) {
        reportCell = [[HJReportCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [reportCell loadMoreData:self.titleArray[indexPath.row].name];
    
    if (indexPath.row == self.cellRow.row) {
        reportCell.IsSelecte = YES;
    }
    if (!reportCell.IsSelecte) {
        reportCell.selectBtn.image = setImage(@"icon_dongtai_manage_unselected");
    }else {
        reportCell.selectBtn.image = setImage(@"icon_dongtai_manage_selected");
    }
    
    return reportCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJReportCell * cell = (HJReportCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell.IsSelecte) {
        return;
    }else {
        cell.IsSelecte = YES;
        cell.selectBtn.image = setImage(@"icon_dongtai_manage_selected");
        if (self.cellRow) {
            HJReportCell * cell1 = (HJReportCell *)[tableView cellForRowAtIndexPath:self.cellRow];
            cell1.IsSelecte = NO;
            cell1.selectBtn.image = setImage(@"icon_dongtai_manage_unselected");
        }
    }
    self.cellRow = indexPath;
}


-(void)setUpBottomView {
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"提交" forState:0];
    btn.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
    [self.view setupShadowWithView:btn];
    [btn setBackgroundColor:[UIColor blackColor]];
    btn.frame = CGRectMake(10, self.view.bounds.size.height - 43 - Height_NavBar-60, self.view.bounds.size.width-20, 43);
    @weakify(self)
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        NSString * url = [Api stringByAppendingString:reportUserApi];
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"reportUserId"] = self.reportUserId;
        params[@"userId"] = [HJUserManager getUserModel].userId;
        params[@"content"] = self.titleArray[self.cellRow.row].name;
        params[@"typeId"] = self.titleArray[self.cellRow.row].typeId;
        
        [HJHttpTool post:url params:params success:^(HJResponeds *result) {
            [self showSuccessHUDWithStr:@"举报成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        } failure:^(id result) {
            [self showErrorHUDWithError:result];
        }];
    }];
    [self.view insertSubview:btn aboveSubview:self.tableView];
}


@end


@implementation HJReportCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubviews];
    }
    return self;
}

-(void)initSubviews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    UIView * bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    self.bgView = bgView;
    [self.contentView addSubview:bgView];
    
    UILabel * label = [UILabel new];
    label.font = [UIFont systemFontOFSize13];
    label.textColor = [UIColor colorWithHex:0x000000];
    label.textAlignment = NSTextAlignmentLeft;
    self.title = label;
    [self.bgView addSubview:label];
    
    UIImageView * mainImageView = [UIImageView new];
    self.selectBtn = mainImageView;
    mainImageView.image = setImage(@"icon_dongtai_manage_unselected");
    [self.bgView addSubview:mainImageView];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    @weakify(self)
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(@6);
        make.left.equalTo(self.contentView).with.offset(10);
        make.bottom.equalTo(self.contentView).with.inset(6);
        make.right.equalTo(self.contentView).with.inset(10);
        [self.contentView setupShadowWithView:self.bgView];
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.equalTo(self.bgView).with.offset(12);
        make.centerY.mas_equalTo(self.bgView);
        make.right.equalTo(self.selectBtn.mas_left).with.inset(12);
        make.height.mas_equalTo(30);
    }];
    
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.right.equalTo(self.bgView).with.inset(12);
        make.centerY.mas_equalTo(self.bgView);
        make.size.mas_equalTo(CGSizeMake(self.selectBtn.image.size.width, self.selectBtn.image.size.height));
    }];
}

-(void)loadMoreData:(NSString *)name {
    self.title.text = name;
}



@end
